//
//  MyInfosViewController.m
//  YYZB
//
//  Created by 顾悦 on 16/3/21.
//
//

#import "MyInfosViewController.h"

#import "ClientWebViewController.h"

#import "ThemeColor.h"

static NSString * HTMLURL = @"user/userCenter.html";

@interface MyInfosViewController ()

@property (strong, nonatomic) UIBarButtonItem *settingBarButtonItem;
@property (strong, nonatomic) UIBarButtonItem *settingIconBarButtonItem;

@property (strong, nonatomic) UIBarButtonItem *fixedSpaceBarButtonItem;

@end

@implementation MyInfosViewController

#pragma mark - 

- (UIBarButtonItem *)settingBarButtonItem {
    if (!_settingBarButtonItem) {
        _settingBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(settingBarButtonItemTapped)];
    }
    
    return _settingBarButtonItem;
}

- (UIBarButtonItem *)settingIconBarButtonItem {
    if (!_settingIconBarButtonItem) {
        _settingIconBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"设置"] style:UIBarButtonItemStylePlain target:self action:@selector(settingBarButtonItemTapped)];
    }
    
    return _settingIconBarButtonItem;
}

- (UIBarButtonItem *)fixedSpaceBarButtonItem {
    if (!_fixedSpaceBarButtonItem) {
        _fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        _fixedSpaceBarButtonItem.width = -8.0;
    }
    
    return _fixedSpaceBarButtonItem;
}

#pragma mark -

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"个人中心";
    self.HTMLURL = HTMLURL;
    
    [self.navigationController.navigationBar setBackgroundImage:UIImage.new forBarMetrics: UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = UIImage.new;
    
    self.navigationItem.rightBarButtonItems = @[self.settingIconBarButtonItem/*, self.fixedSpaceBarButtonItem, self.settingBarButtonItem*/];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLogout) name:@"NotificationNameUserLogout" object:nil];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"NotificationNameUserLogout" object:nil];
}

- (void)userLogout {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.navigationController popToRootViewControllerAnimated:NO];
        self.tabBarController.selectedIndex = 0;
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 

- (void)settingBarButtonItemTapped {
    ClientWebViewController *cw = ClientWebViewController.new;
    cw.hidesBottomBarWhenPushed = YES;
    cw.title = @"设置";
    cw.HTMLURL = @"user/setting.html";
    [self.navigationController pushViewController:cw animated:YES];

//    [self.webView evaluateJavaScript:@"" completionHandler:^(id _Nullable u, NSError * _Nullable error) {
//        
//    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
